import matplotlib.pyplot as plt
import pandas as pd
from dataidea.datasets import loadDataset
Matplotlib
# Load the Titanic dataset
= loadDataset('../assets/demo_cleaned.csv', inbuilt=False, file_type='csv') demo_df
Create a plot
= plt.figure()
fig # All plotting is done with respect to an Axes.
0.1, 0.1, 0.5, 0.5]) fig.add_axes([
In most cases, a subplot will fit your needs. A subplot is an axes on a grid system.
= plt.subplots()
fig1, ax
ax.hist(demo_df.income) plt.show()
= plt.subplots()
fig2, ax
ax.hist(demo_df.income)# plt.grid(True)
= fig2.add_subplot(222) # row-col-num
ax2 'age'])
ax2.hist(demo_df[ plt.show()
= plt.subplots(nrows=2,ncols=2) fig3, axes
= plt.subplots(nrows=2,ncols=2)
fig4, axes # add bar graph
= demo_df.gender.value_counts()
gender_counts 0,0].bar(gender_counts.index, gender_counts.values)
axes[# add histogram
0,1].hist(demo_df.age, bins=20, edgecolor='black')
axes[# add box plot
1, 0].boxplot(demo_df.income, vert=0)
axes[# add scatter plot
1, 1].scatter(demo_df.age, demo_df.income)
axes[ plt.show()
2D Data or Images
from PIL import Image
= Image.open('../assets/dataidea-logo.png')
image
= plt.subplots()
fig4, ax
ax.imshow(image) plt.show()
Save Figure
= plt.subplots(nrows=2,ncols=2)
fig4, axes # add bar graph
= demo_df.gender.value_counts()
gender_counts 0,0].bar(gender_counts.index, gender_counts.values)
axes[# add histogram
0,1].hist(demo_df.age, bins=20, edgecolor='black')
axes[# add box plot
1, 0].boxplot(demo_df.income, vert=0)
axes[# add scatter plot
1, 1].scatter(demo_df.age, demo_df.income)
axes[
'figure.pdf')
plt.savefig( plt.show()
<Figure size 640x480 with 0 Axes>